/*
 * 天虹商场股份有限公司版权所有.
 */
package com.tianhong.stuqueue;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

/**
 * TODO
 *
 * @author 黄明坤
 * @version 1.0.0
 * @date 2020/4/20
 */
public class ArrayBlockingQueueTest {

    public ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(1);

    public static void main(String[] args) throws InterruptedException {
        /*Queue<String> queue = new LinkedList<String>();
        ConcurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue();
        concurrentLinkedQueue.add("123");

        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(10);
        arrayBlockingQueue.add(1);
        arrayBlockingQueue.add(2);
        arrayBlockingQueue.add(5);
        arrayBlockingQueue.add(6);
        arrayBlockingQueue.put(1);
        Object rst = arrayBlockingQueue.element();
        System.out.println("查看:"+rst);
        Stream stream = arrayBlockingQueue.stream();
        stream.forEach(o -> System.out.println(o));
        System.out.println("------------分割线----");
        arrayBlockingQueue.offer(9);
        stream = arrayBlockingQueue.stream();

        stream.forEach(o -> System.out.println(o));
        System.out.println("------------分割线----");

        System.out.println(arrayBlockingQueue.peek());*/
        System.out.println(Thread.currentThread().getName()+":start");
        ArrayBlockingQueueTest arrayBlockingQueueTest = new ArrayBlockingQueueTest();
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+":start");
                arrayBlockingQueueTest.testArrayBlockingQueue();
                System.out.println(Thread.currentThread().getName()+":end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        Thread.sleep(1000);
        arrayBlockingQueueTest.arrayBlockingQueue.peek();
        System.out.println(Thread.currentThread().getName()+":end");
    }


    public void testArrayBlockingQueue () throws InterruptedException {

      /*  arrayBlockingQueue.poll(1l, TimeUnit.SECONDS);
        arrayBlockingQueue.poll();*/
        arrayBlockingQueue.add("test1");
        arrayBlockingQueue.put("test2");



        /*LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue();
        linkedBlockingQueue.poll();*/
        //poll(time)：取走 BlockingQueue 里排在首位的对象，若不能立即取出，则可以等 time 参数规定的时间,取不到时返回 null
        //take()：取走 BlockingQueue 里排在首位的对象，若 BlockingQueue 为空，阻断进入等待状态直到 Blocking 有新的对象被加入为止
    }
}
